/**
 * 功能描述 散客团信息导航条
 */
define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Subnav = require("subnav");
	var enmu = require("enums");
	var Dialog = require("dialog");
	var user = require("store").get("user");
	
	var subnavEvents = require("../handler/subnavEvents");
	
    var instance = {
        initComponent: function (params, widget) {
            var subnav = new Subnav({
            	parentNode : ".J-subnav",
            	model : {
        			title : "散客预约",
        			items:[{
					    id:"servicePoint",
					    type : "buttongroup",
					    all:{
					      show:false,
					      first:true
					    },
					    tip:"服务点",
					    keyField :"pkServicePoint",
					    valueField :"name",
					    items:user.privateServicePoints,
					    params: function(){
					      return{
					    	  fetchProperties:"pkServicePoint,name"
					      }
					    },
					    handler:function(key,element){
					      widget.get("grid").refresh();
					    }
				    },{
        				id : "status",
        				type : "buttongroup",
        				tip : "状态",
        				items : [{
							key:"All",
							value:"全部"
						},{
							key:"Initial",
							value:"待确认",
						},{
							key:"Confirm",
							value:"已确认"
						},{
							key:"Reserve",
							value:"已预约"
						},{
							key:"Effective",
							value:"有效"
						},{
							key:"CheckIn",
							value:"入住"
						}],
        				handler: function(key,element){
        					widget.get("grid").refresh();
        				}
        			},{
						id : "startDate",
						type : "daterange",
						ranges : {
					        "本年": [moment().startOf("year"), moment().endOf("days")],
					        "本月": [moment().startOf("month"), moment().endOf("month")]
						},
						tip : "入住日期",
						handler : function(time){
							 widget.get("grid").refresh();
						}
					},{
        				id:"add",
        				type:"button",
        				text:"新增",
        				handler:function(){
        					widget.get("form").reset();
        					widget.get("pereditgrid").setData([]);
        					widget.show([".J-form",".J-pereditgrid"]).hide([".J-grid"]);
							widget.get("subnav").hide(["add", "servicePoint", "status","startDate"]).show(["save", "return"]);
        				}
        			},{
        				id : "save",
						type : "button",
						text : "保存",						
						show : false,
						handler:function(){
							subnavEvents.saveEditgrid(widget);
						}
        			},{
        				id : "return",
						type : "button",
						text : "返回",						
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-form",".J-pereditgrid"]);
							widget.get("subnav").hide(["save", "return"]).show(["servicePoint", "add","status", "startDate"]);
							widget.get("grid").refresh();
						}
        			},{
        				id : "reserve",
						type : "button",
						text : "预约完成",						
						show : false,
						handler:function(){
							var editgridData = widget.get("editgrid").getData();
							var signUpData = [];
							for(var i in editgridData){
								var signUp = {
									count : editgridData[i].count,
									payables : editgridData[i].payables,
									roomStatus : "Pending"
								}
								if(typeof editgridData[i].roomType != "string"){
									signUp["roomType"] = editgridData[i].roomType.pkRoomType;
								}else{
									signUp["roomType"] = editgridData[i].roomType;
								}
								signUpData.push(signUp);
							}
							if(signUpData.length == 0){
								Dialog.alert({
									tip : "提示",
									content : "未选择预约房型！"
								});
								return false;
							}else{
								var saveData = {
										nonMemberGroup : {
											pkNonMemberGroup : widget.get("pkNonMemberGroup"),
											status : "Reserve"
										},
										signUpRooms : signUpData
								}
								aw.ajax({
									url : "api/nonmemgroupreserve/reserve",
									data : aw.customParam(saveData),
									dataType : "json",
									success : function(datas){
										widget.show([".J-grid"]).hide([".J-editgrid"]);
										widget.get("subnav").hide(["sure", "reserve", "cancel"]).show(["status","servicePoint", "startDate", "add"]);
										widget.get("grid").refresh();
									}
								})
							}
						}
        			},{
        				id : "sure",
						type : "button",
						text : "确认",						
						show : false,
						handler:function(){
							var editgridData = widget.get("editgrid").getData();
							var signUpData = [];
							for(var i in editgridData){
								var signUp = {
									count : editgridData[i].count,
									payables : editgridData[i].payables,
									roomStatus : "Pending"
								}
								if(typeof editgridData[i].roomType != "string"){
									signUp["roomType"] = editgridData[i].roomType.pkRoomType;
								}else{
									signUp["roomType"] = editgridData[i].roomType;
								}
								signUpData.push(signUp);
							}
							if(signUpData.length == 0){
								Dialog.alert({
									tip : "提示",
									content : "未选择预约房型！"
								});
								return false;
							}else{
								var saveData = {
										nonMemberGroup : {
											pkNonMemberGroup : widget.get("pkNonMemberGroup"),
											status : "Confirm"
										},
										signUpRooms : signUpData
								}
								aw.ajax({
									url : "api/nonmemgroupreserve/reserve",
									data : aw.customParam(saveData),
									dataType : "json",
									success : function(datas){
										widget.show([".J-grid"]).hide([".J-editgrid"]);
										widget.get("subnav").hide(["sure", "reserve", "cancel"]).show(["status","servicePoint", "startDate", "add"]);
										widget.get("grid").refresh();
									}
								})
							}
						}
        			},{
        				id : "cancel",
						type : "button",
						text : "取消",						
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-editgrid"]);
				        	widget.get("subnav").hide(["sure", "reserve", "cancel"]).show(["status","servicePoint", "startDate", "add"]);
						}
        			}]
        		}
            })
        	widget.set("subnav", subnav);
        }
    };
    module.exports = instance;
});
